﻿using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Windows.Forms;

namespace Helpers
{

    public class LogHelper
    {
        /// <summary>
        /// 控制级别，由低到高
        /// </summary>
        public enum Level { ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF }

        private static readonly ILog log = LogManager.GetLogger("root");

        //private static readonly ILog logerror = LogManager.GetLogger("logerror");
        public static void SetConfig()
        {
            log4net.Config.XmlConfigurator.Configure();
        }
        public static void SetConfig(FileInfo fileInfo)
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(fileInfo);
        }
        public static void Log(string msg, Level level = Level.INFO, Exception ex = null)
        {
            switch (level)
            {
                case Level.DEBUG:
                    Debug(msg);
                    break;
                case Level.INFO:
                    Info(msg);
                    break;
                case Level.WARN:
                    Warn(msg);
                    break;
                case Level.ERROR:
                    Error(msg, ex);
                    break;
                case Level.FATAL:
                    Fatal(msg);
                    break;
                default:
                    break;
            }
        }
        public static void Info(string user, string msg)
        {
            Info($"【名称】{user}【内容】{msg}");
        }
        public static void Info(string msg)
        {
            if (log.IsInfoEnabled)
            {
                log.Info(msg);
            }
        }
        /// <summary>
        /// 警告
        /// </summary>
        /// <param name="msg"></param>
        public static void Warn(string msg)
        {

            if (log.IsWarnEnabled)
            {
                log.Warn(msg);
            }
        }
        public static void Error(string user, string msg, Exception ex)
        {
            Error($"【用户名】{user}【内容】{msg}", ex);
        }
        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ex"></param>
        public static void Error(string msg, Exception ex)
        {

            if (log.IsErrorEnabled)
            {
                log.Error(msg, ex);
            }
        }
        /// <summary>
        /// 调试
        /// </summary>
        /// <param name="msg"></param>
        public static void Debug(string msg)
        {

            if (log.IsDebugEnabled)
            {
                log.Debug(msg);
            }
        }
        /// <summary>
        /// 致命
        /// </summary>
        /// <param name="msg"></param>
        public static void Fatal(string msg)
        {

            if (log.IsFatalEnabled)
            {
                log.Fatal(msg);
            }
        }

        ///// <summary>
        ///// 只输出到name="logerror"的logger
        ///// </summary>
        ///// <param name="info"></param>
        ///// <param name="se"></param>
        //public static void WriteLogError(string info, Exception se)
        //{
        //    if (logerror.IsErrorEnabled)
        //    {
        //        logerror.Error(info, se);
        //    }
        //}

        /// <summary>
        /// 捕获winform未处理的异常
        /// </summary>
        public static void BindExceptionHandler()
        {
            //设置应用程序处理异常方式：ThreadException处理
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理未捕获的异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }
        /// <summary>
        /// 处理UI线程异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Error(e.Exception.StackTrace, e.Exception);
        }
        /// <summary>
        /// 处理未捕获的异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Error((e.ExceptionObject as Exception)?.StackTrace, e.ExceptionObject as Exception);
        }


        /// <summary>
        /// 捕获asp.net未处理的异常
        /// </summary>
        /// <param name="httpContext"></param>
        public static void ApplicationError(HttpContext httpContext)
        {
            Exception objExp = HttpContext.Current.Server.GetLastError();
            Error("【客户机IP】：" + HttpContext.Current.Request.UserHostAddress + "【错误地址】：" + HttpContext.Current.Request.Url, objExp);
        }


    }
}

